package com.ding.custommanagersys.controller;

import com.ding.custommanagersys.entity.*;
import com.ding.custommanagersys.service.IAreaService;
import com.ding.custommanagersys.service.ICustomService;
import com.ding.custommanagersys.service.IEmailService;
import com.ding.custommanagersys.service.IMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;


/**
 * @author Miles
 * @description: 用作页面跳转使用
 * @date 2022/11/25 16:38
 **/
@Controller
public class PageUriController {

    @Autowired
    private IAreaService areaService;

    @Autowired
    private IMemberService memberService;

    @Autowired
    private ICustomService customService;

    @Autowired
    private IEmailService emailService;

    //跳转到登录页面
    @RequestMapping(value = {"/login","/","/index"})
    public String login(){
        return "index";
    }

    //跳转到注册页面
    @RequestMapping("/register")
    public String register(){
        return "register";
    }

    //注册成功后，跳转到登录页面，并且附带用户名
    @RequestMapping(value = {"/registerSucess"})
    public String registerSucess(@RequestParam("username") String username,
                                 Model model){
        model.addAttribute("username",username);
        return "index";
    }

    //跳转到后台主页
    @RequestMapping("/admin")
    public String admin(){
        return "admin";
    }

    //后台欢迎页面
    @RequestMapping("/welcome")
    public String hello(){
        return "welcome";
    }

    //后台table页
    @RequestMapping("/areaTable")
    public String table(){
        return "areaTable";
    }

    //后台table页下的添加页面add.html
    @RequestMapping("/table/areaAdd")
    public String add(){
        return "table/areaAdd";
    }

    //后台table页下的添加页面edit.html
    @RequestMapping("/table/areaEdit/{id}")
    public String edit(@PathVariable("id") int tId, Model model){
        Area oneArea = areaService.getOneArea(tId);
        model.addAttribute("area",oneArea);
        return "table/areaEdit";
    }

    // ============================================
    //后台table页
    @RequestMapping("/memberTable")
    public String memberTable(){
        return "memberTable";
    }

    //会员等级添加页面
    @RequestMapping("/table/memberAdd")
    public String memberAdd(){
        return "table/memberAdd";
    }

    //会员后台table页下的修改页面edit.html
    @RequestMapping("/table/memberEdit/{id}")
    public String memberEdit(@PathVariable("id") int id, Model model){
        Member oneMember = memberService.getOneMember(id);
        model.addAttribute("member",oneMember);
        return "table/memberEdit";
    }


    // ============================================
    //客户 table页
    @RequestMapping("/customTable")
    public String customTable(){
        return "customTable";
    }

    //客户 添加页面
    @RequestMapping("/table/customAdd")
    public String customAdd(Model model){
        List<Member> list = memberService.listMember();
        model.addAttribute("memberList",list);
        return "table/customAdd";
    }

    //客户后台table页下的修改页面customEdit.html
    @RequestMapping("/table/customEdit/{id}")
    public String customEdit(@PathVariable("id") String id, Model model){
        Custom oneCustom = customService.getOneCustom(id);
        List<Member> list = memberService.listMember();
        Area area = areaService.getOneArea(oneCustom.gettAreaId());
        model.addAttribute("memberList",list);
        model.addAttribute("area",area);
        model.addAttribute("custom",oneCustom);

        return "table/customEdit";
    }



    //用户点击用户信息显示页面内
    @RequestMapping("/userAdmin")
    public String customEdit(Model model, HttpSession session){
        User user = (User) session.getAttribute("user");
        String id = user.gettUserId();

        Custom oneCustom = customService.getOneCustom(id);
        List<Member> list = memberService.listMember();
        Area area = areaService.getOneArea(oneCustom.gettAreaId());
        model.addAttribute("area",area);
        model.addAttribute("memberList",list);
        model.addAttribute("custom",oneCustom);

        return "userAdmin";
    }

    // ============================================
    //管理员发送邮件
    @RequestMapping("/sendMsg")
    public String sendMsg(){
        return "sendMsg";
    }

    //用户接收邮箱
    @RequestMapping("/userEmail")
    public String userEmail(){
        return "userEmail";
    }

    //邮件详情查看
    @RequestMapping("/getDetailEmail/{emailId}")
    public String getDetailEmail(Model model,
                                 @PathVariable("emailId") String emailId){
        Email email = emailService.getOneEmail(emailId);
        model.addAttribute("email",email);
        return "userReadEmail";
    }

}
